/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package utn.tsb.tej.world_logic;

import java.io.Serializable;
import csm.uttils.generics.Roster;

/**
 *
 * @author Administrador
 */
public class Player implements Comparable, Serializable {

    public static int TYPE_HUMAN = 0, TYPE_CPU = 1;

    private String nickName;
    private String avatar;
    private Color color;
    private Roster<Country> countries;
    private Roster<Card> cards;
    private int changes;
    private Roster<Objective> objectives;
    private boolean conquered, ready;
    private int roll, type;
    private String address;

    public int getRoll() {
        return roll;
    }

    public void setRoll(int roll) {
        this.roll = roll;
    }

    public Player(String nickName, String avatar, Color color, Roster countries, Roster cards, int changes, Roster objectives, boolean conquered) {
        this.nickName = nickName;
        this.avatar = avatar;
        this.color = color;
        this.countries = countries;
        this.cards = cards;
        this.changes = changes;
        this.objectives = objectives;
        this.conquered = conquered;
        this.roll=0;
    }

    public Player() {
        countries = new Roster<Country>();
        cards = new Roster<Card>();
        objectives = new Roster<Objective>();
    }

    public String getAvatar() {
        return avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }

    public Roster<Card> getCards() {
        return cards;
    }

    public void setCards(Roster cards) {
        this.cards = cards;
    }

    public int getChanges() {
        return changes;
    }

    public void setChanges(int changes) {
        this.changes = changes;
    }

    public Color getColor() {
        return color;
    }

    public void setColor(Color color) {
        this.color = color;
    }

    public boolean isConquered() {
        return conquered;
    }

    public void setConquered(boolean conquered) {
        this.conquered = conquered;
    }

    public Roster<Country> getCountries() {
        if(countries==null){
            countries=new Roster<Country>();
        }
        return countries;
    }

    public void setCountries(Roster countries) {
        this.countries = countries;
    }

    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }

    public Roster<Objective> getObjectives() {
        return objectives;
    }

    public void setObjectives(Roster<Objective> objectives) {
        this.objectives = objectives;
    }

    @Override
    public int compareTo(Object o) {
        if (!(o instanceof Player)) {
            return -1;
        }
        Player p = (Player) o;
        int dif = this.color.getCodeRGB().compareTo(p.getColor().getCodeRGB());
        return dif;

    }

    public boolean isOwnerOfCountry(Country country) {
        boolean owner = false;
        if (countries.indexOf(country) != -1) {
            owner = true;
        }
        return owner;

    }

    public boolean isOwnerOfContinent(Continent continent) {
        boolean owner = true;
        Roster<Country> cCountries = continent.getCountries();
        for (int i = 0; i < cCountries.count(); i++) {
            if (!this.isOwnerOfCountry(cCountries.getElementInPosition(i))) {
                owner = false;
                break;
            }
        }
        return owner;
    }

    public boolean addArmy(Country country) {
        if (!isOwnerOfCountry(country)) {
            return false;
        }
        int p = countries.indexOf(country);
        countries.getElementInPosition(p).addArmie(1);
        return true;
    }

    public boolean moveArmies(Country from, Country to, int amount) {
        return false;//TODO
    }

    public int getTotalArmies() {
        int amnt = 0;
        for (int i = 0; i < countries.count(); i++) {
            amnt += countries.getElementInPosition(i).getArmies();
        }
        return amnt;
    }

    public boolean objectivesAccomplished() {
        boolean out = true;
        for (int i = 0; i < objectives.count(); i++) {
            if (!objectives.getElementInPosition(i).isAccomplished()) {
                out = false;
            }
            break;
        }
        return out;
    }

    public String toString(){
        String out="";
        out+=nickName;
        out+=" - "+color.toString();
        return out;
    }

    /**
     * @return the ready
     */
    public boolean isReady() {
        return ready;
    }

    /**
     * @param ready the ready to set
     */
    public void setReady(boolean ready) {
        this.ready = ready;
    }

    /**
     * @return the type
     */
    public int getType() {
        return type;
    }

    /**
     * @param type the type to set
     */
    public void setType(int type) {
        this.type = type;
    }

    /**
     * @return the address
     */
    public String getAddress() {
        return address;
    }

    /**
     * @param address the address to set
     */
    public void setAddress(String address) {
        this.address = address;
    }
}
